<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Common\Api;
use Common\Api\Api;

class ModelApi extends Api{

    /* 构造方法 */
    protected function init(){
        $this->db = D('Model');
        $this->model_db = D('ModelField');
        $this->error = '';
    }

    /* 更新模型缓存 */
    public function build_cache($modelid = 0) {
        $modelid = (int) $modelid;
        $sqlMap = array();
        if ($modelid > 0) {
            $sqlMap['modelid'] = $modelid;
        }
        $lists = $this->db->select();
        $models = array();
        foreach ($lists as $r) {
            $models[$r['modelid']] = $r;
            $model_fields = $this->model_db->where(array('modelid' => $r['modelid'], 'disabled' => '0'))->order('listorder ASC')->select();
            $fields = array();
            foreach($model_fields as $field) {
                $fields[$field['field']] = $field;
            }
            if (!empty($fields)) {
                setcache('model_field_'.$r['modelid'], $fields, 'model');
            } else {
                delcache('model_field_'.$r['modelid'], 'model');
            }            
        }
        return setcache('model', $models, 'commons');
    }
}